package com.machine.filling.function.init.OilGunList;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.machine.filling.R;
import com.machine.filling.net.http.response.OilGunListResBean;
import com.machine.filling.ui.dialog.NoticeDialog;

import java.util.List;

/**
 * Created by Administrator on 2018/1/14 0014.
 */

public class OilGunListAdapter extends RecyclerView.Adapter<OilGunListAdapter.Holder> {
    private Context mContext;
    private List<OilGunListResBean.ResultBean.GunsBean> datas;
    private IClickItemListener iClickItemListener;

    public OilGunListAdapter(Context mContext, List<OilGunListResBean.ResultBean.GunsBean> datas) {
        this.mContext = mContext;
        this.datas = datas;
    }

    public void setiClickItemListener(IClickItemListener iClickItemListener) {
        this.iClickItemListener = iClickItemListener;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_add_gun_blank, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        if (datas.size() > position) {
            holder.tv_gun_name.setText(datas.get(position).getName());
            holder.rl_added.setVisibility(View.VISIBLE);
            holder.rl_balnk.setVisibility(View.GONE);
        }else {
            holder.rl_balnk.setVisibility(View.VISIBLE);
            holder.rl_added.setVisibility(View.GONE);
        }

        holder.tv_modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iClickItemListener.clickModifyOrAdd(datas.get(position));
            }
        });

        holder.tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final NoticeDialog  noticeDialog = new NoticeDialog(mContext);
                noticeDialog.setMyTitle("提示");
                noticeDialog.setMyContent("确定删除该油枪？");
                noticeDialog.setiClickListener("确认", "取消", new NoticeDialog.IClickListener() {
                    @Override
                    public void clickLeft() {
                        noticeDialog.dismiss();
                        iClickItemListener.clickDelete(position,datas.get(position));
                    }

                    @Override
                    public void clickRight() {
                        noticeDialog.dismiss();
                    }
                });
                noticeDialog.show();

            }
        });

        holder.rl_balnk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iClickItemListener.clickModifyOrAdd(null);
            }
        });

        holder.rl_added.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iClickItemListener.clickModifyOrAdd(datas.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return 2;
    }

    public class Holder extends RecyclerView.ViewHolder {

        private final RelativeLayout rl_balnk;
        private final RelativeLayout rl_added;
        private final TextView tv_gun_name;
        private final TextView tv_modify;
        private final TextView tv_del;

        public Holder(View itemView) {
            super(itemView);
            rl_balnk = (RelativeLayout) itemView.findViewById(R.id.rl_balnk);
            rl_added = (RelativeLayout) itemView.findViewById(R.id.rl_added);
            tv_gun_name = (TextView) itemView.findViewById(R.id.tv_gun_name);
            tv_modify = (TextView) itemView.findViewById(R.id.tv_modify);
            tv_del = (TextView) itemView.findViewById(R.id.tv_del);

        }
    }

    public interface IClickItemListener {

        /**
         * 修改获取添加油枪信息
         */
        void clickModifyOrAdd(OilGunListResBean.ResultBean.GunsBean bean);

        /**
         * 删除油枪
         */
        void clickDelete(int position,OilGunListResBean.ResultBean.GunsBean bean  );
    }
}
